package edu.sandau.b230111.b23011125.Service.Impl;

import edu.sandau.b230111.b23011125.Global.GlobalEnum;
import edu.sandau.b230111.b23011125.Model.History;
import edu.sandau.b230111.b23011125.Model.SubmitRecord;
import edu.sandau.b230111.b23011125.Repository.HistoryRepository;
import edu.sandau.b230111.b23011125.Service.HistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class HistoryServiceImpl implements HistoryService {
    @Autowired
    private HistoryRepository historyRepository;

    @Override
    public GlobalEnum.OperateResult modifyHistory(String userid, String problemid, SubmitRecord record) {
        History history = historyRepository.findByUseridAndProblemid(userid,problemid);
        if(history==null){
            history = new History();
            history.setHistoryid(UUID.randomUUID().toString());
            history.setUserid(userid);
            history.setProblemid(problemid);
            history.setStatus(0);
        }
        if(history.getStatus()==2){
            return GlobalEnum.OperateResult.SUCC;
        }
        if(record.getStatus().equals("通过")){
            history.setStatus(2);
        }
        else{
            history.setStatus(1);
        }
        historyRepository.save(history);
        return GlobalEnum.OperateResult.SUCC;
    }

    @Override
    public History queryHistory(String userid, String problemid) {
        History history = historyRepository.findByUseridAndProblemid(userid,problemid);
        if(history==null){
            history = new History();
            history.setHistoryid(UUID.randomUUID().toString());
            history.setUserid(userid);
            history.setProblemid(problemid);
            history.setStatus(0);
        }
        historyRepository.save(history);
        return history;
    }
}
